//
//  AY_LoadingView.m
//  AiYue
//
//  Created by mac on 2023/11/9.
//

#import "AY_LoadingView.h"

@interface AY_LoadingView ()
@property (nonatomic, strong) UIButton *refreshBtn;
@property (nonatomic, strong) UIView *refreshView;
@property (nonatomic,strong) UIButton *backBtn;
@end

@implementation AY_LoadingView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.customImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        self.customImageView.center = CGPointMake(CGRectGetWidth(self.frame)/2, CGRectGetHeight(self.frame)/2 - 100);
        self.customImageView.bounds = CGRectMake(0, 0, 60, 60);
        self.customImageView.animationImages = [self animationImages];
        self.customImageView.animationDuration = 2;     //执行一次完整动画所需的时长
        self.customImageView.animationRepeatCount = 0;  //动画重复次数
        [self.customImageView startAnimating];
        [self addSubview:self.customImageView];
        self.hideLeftpop = YES;
        [self addSubview:self.backBtn];
        
        self.infoLab = [[UILabel alloc] init];
        self.infoLab.font = YFont(14);
        self.infoLab.textColor = COLOR(@"666666");
        self.infoLab.text = @"正在努力加载中...";
        [self addSubview:self.infoLab];
        [self.infoLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.customImageView);
            make.top.equalTo(self.customImageView.mas_bottom).offset(YNUM(10));
        }];
        
        self.refreshView = [AYTool createViewWithFrame:CGRectZero andBg:nil andRadius:0 andAddSubView:self];
        self.refreshView.hidden = YES;
        [self.refreshView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.top.equalTo(self.customImageView);
            make.width.mas_equalTo(YNUM(150));
            make.height.mas_equalTo(YNUM(150));
        }];
        
        UIImageView *img = [AYTool createImageViewWithFrame:CGRectZero andImage:YImage(@"AY_error") andBg:nil andRadius:0 andAddSubView:self.refreshView];
        [img mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.top.equalTo(self.refreshView);
            make.height.width.mas_equalTo(YNUM(100));
        }];
        
        UILabel *refreshLab = [AYTool createLabelWithFrame:CGRectZero andText:@"网络出错，请点击重试" andTextColor:COLOR(@"666666") andBgColor:nil andFont:YFont(14) andLines:1 andRadius:0 andAddSubView:self.refreshView];
        [refreshLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.refreshView);
            make.top.equalTo(img.mas_bottom).offset(YNUM(10));
        }];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(refreshAction)];
        [self.refreshView addGestureRecognizer:tap];
    }
    return self;
}

- (void)setHideLeftpop:(BOOL)hideLeftpop {
    _hideLeftpop = hideLeftpop;
    self.backBtn.hidden = hideLeftpop;
}

- (void)refreshAction {
    if ([self.delegate respondsToSelector:@selector(refreshAction)]) {
        [self.delegate refreshAction];
    }
}

- (void)pop {
    if ([self.delegate respondsToSelector:@selector(popAction)]) {
        [self.delegate popAction];
    }
}

- (void)dismiss {
    [self removeFromSuperview];
}

- (void)loadingInView:(UIView *)view {
    [view addSubview:self];
}

- (void)refresh {
    self.refreshView.hidden = NO;
    self.customImageView.hidden = YES;
    self.infoLab.hidden = YES;
}

- (NSArray *)animationImages
{
    NSMutableArray *imagesArr = [NSMutableArray array];
    for (int i = 0; i < 50; i ++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"xl_%d",i]];
        if (image) {
            [imagesArr addObject:image];
        }
    }
    return imagesArr;
}

- (UIButton *)backBtn {
    if (!_backBtn) {
        UIButton *backBtn = [AYTool createButtonWithFrame:CGRectMake(0, [UIView statusBar], YNUM(40), [UIView navigationBar]) andTitle:nil andFont:nil andTitleColor:nil andBg:nil andImage:YImage(@"AY_nav_pop") andBgImage:nil andTarget:self andAction:@selector(pop) andRadius:0 andAddSubView:self];
        backBtn.hidden = self.hideLeftpop;
        _backBtn = backBtn;
    }
    return _backBtn;
}
@end
